#include "child.h"


void *threadFunc(void *arg)
{
	Port_FileName_Token_t* Node = (Port_FileName_Token_t*)arg;
    // 0. 创建套接字
	int clientfd = socket(AF_INET, SOCK_STREAM, 0);
	// 1. 设置服务器的网络地址
	struct sockaddr_in serverAddr;
	memset(&serverAddr, 0, sizeof(serverAddr));

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(Node->port);
	serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	int ret = connect(clientfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr));

	//验证Token
	sendArray(clientfd,Node->Token,strlen(Node->Token));
	int flag = 404;
	recv(clientfd,&flag,4,MSG_WAITALL);
	if(flag == 404)
	{
		printf("验证失败\n");
		return NULL;
	}
	switch (Node->CmdType)
	{
	case CMD_TYPE_GET:
		//发送指令类型
		send(clientfd,&Node->CmdType,4,MSG_WAITALL);
		//开始传输文件
		getsFile(clientfd,Node->filename);
		break;
	case CMD_TYPE_PUT:
		send(clientfd,&Node->CmdType,4,MSG_WAITALL);
		//开始传输文件
		putsFile(clientfd,Node->filename);
	default:
		break;
	}
}